﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Generator.DB
{
    public class TableInfo
    {

        public IConnection Connection { get; private set; }
        public string TableName { get; set; }

        private List<FieldInfo> _fields;
        public List<FieldInfo> Fields
        {
            get
            {
                if (_fields == null)
                {
                    FillFields();
                }
                return _fields;
            }            
        }

        public bool IsMasterTable
        {
            get
            {
                string pattern=@"[a-zA-Z_][a-zA-Z0-9_]*m_[a-zA-Z0-9_]+";
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                return reg.IsMatch(TableName);
                //if (TableName.ToLower().StartsWith("m_"))
                //    return true;
                //else
                //    return false;
            }
        }

        public bool IsBusinessTable
        {
            get
            {
                string pattern = @"[a-zA-Z_][a-zA-Z0-9_]*t_[a-zA-Z0-9_]+";
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                return reg.IsMatch(TableName);
                //if (TableName.ToLower().StartsWith("t_"))
                //    return true;
                //else
                //    return false;
            }
        }

        public string EntityName
        {
            get
            {
                string s = string.Empty;
                if (IsMasterTable || IsBusinessTable)
                    s = TableName.Substring(2);
                else
                    s = TableName;

                s= string.Format("{0}{1}", s.Substring(0, 1).ToUpper(), s.Substring(1));

                if (this.IsMasterTable)
                    return string.Format("Mst{0}", s);
                else
                    return string.Format("{0}", s);
            }
        }

        public string EntityListName
        {
            get
            {
                return string.Format("{0}List", this.EntityName);
            }
        }

        public string ModelEditName
        {
            get
            {
                return string.Format("{0}Edit", this.EntityName);
            }
        }

        public string ModelListName
        {
            get
            {
                return string.Format("{0}List", this.EntityName);
            }
        }

        public string ViewEditName
        {
            get
            {
                return string.Format("{0}Edit", this.EntityName);
            }
        }

        public string ModelListSearchName
        {
            get
            {
                return string.Format("{0}ListSearch", this.EntityName);
            }
        }

        public string ViewListName
        {
            get
            {
                return string.Format("{0}List", this.EntityName);
            }
        }

        public string ModelEditRepositoryName
        {
            get
            {
                return string.Format("{0}EditRepository", this.EntityName);
            }
        }

        public string ModelListRepositoryName
        {
            get
            {
                return string.Format("{0}ListRepository", this.EntityName);
            }
        }
        public TableInfo()
        {
            TableName = "";
            _fields = null;
        }

        public TableInfo(IConnection connection, string tableName)
        {
            this.Connection = connection;
            TableName = tableName;
            _fields = null;
        }

        private void FillFields()
        {
            _fields =  this.Connection.GetTableField(this);
        }

        //public void AddField(Field field)
        //{
        //    _fields.Add(field);
        //}

        public FieldInfo GetKeyField()
        {
            FieldInfo reValue = null;
            var fields = (from s in Fields where s.Key==true select s);
            foreach (var f in fields)
            {
                reValue = f;
                break;
            }

            if (reValue == null)
                reValue = Fields[0];

            return reValue;

        }

        public FieldInfo GetDisplayValueField()
        {
            FieldInfo reValue = null;
            var fields = (from s in Fields where s.Key==false && s.FieldType.MapName.CompareTo("string")==0 select s);
            foreach (var f in fields)
            {
                reValue = f;
                break;
            }

            if (reValue == null)
            {
                if (Fields.Count >1)
                    reValue = Fields[1];
                else if (Fields.Count > 0)
                    reValue = Fields[0];
            }

            return reValue;

        }

        public FieldInfo FindField(string fieldName)
        {
            return Fields.Find(m => m.FieldName.ToLower().CompareTo(fieldName.ToLower()) == 0);
        }

        public override string ToString()
        {
            return TableName;
        }

    }
}
